package org.jeecg.modules.online.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.online.auth.entity.OnlAuthRelation;
import org.jeecg.modules.online.auth.mapper.OnlAuthRelationMapper;
import org.jeecg.modules.online.auth.service.IOnlAuthRelationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: onl_auth_relation
 * @Author: jeecg-boot
 * @Date:   2020-07-16
 * @Version: V1.0
 */
@Service("onlAuthRelationServiceImpl")
public class OnlAuthRelationServiceImpl extends ServiceImpl<OnlAuthRelationMapper, OnlAuthRelation> implements IOnlAuthRelationService {

    @Override
    public void saveRoleAuth(String roleId, String cgformId, int type, String authMode, List<String> authIds) {
        //1 首先删除所有的
        LambdaQueryWrapper<OnlAuthRelation> query = new LambdaQueryWrapper<OnlAuthRelation>()
                .eq(OnlAuthRelation::getCgformId, cgformId)
                .eq(OnlAuthRelation::getType, type)
                .eq(OnlAuthRelation::getAuthMode, authMode)
                .eq(OnlAuthRelation::getRoleId, roleId);
        this.baseMapper.delete(query);

        //循环构造实体
        List<OnlAuthRelation> ls = new ArrayList<OnlAuthRelation>();
        for(String authId: authIds){
            OnlAuthRelation temp = new OnlAuthRelation();
            temp.setAuthId(authId);
            temp.setCgformId(cgformId);
            temp.setRoleId(roleId);
            temp.setType(type);
            temp.setAuthMode(authMode);
            ls.add(temp);
        }
        //批量插入
        this.saveBatch(ls);
    }
}
